package com.ruoyi.web.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Performance;
import com.ruoyi.system.service.IPerformanceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学生成绩Controller
 * 
 * @author ruoyi
 * @date 2024-05-23
 */
@RestController
@RequestMapping("/system/performance")
public class PerformanceController extends BaseController
{
    @Autowired
    private IPerformanceService performanceService;

    /**
     * 查询学生成绩列表
     */
    @PreAuthorize("@ss.hasPermi('system:performance:list')")
    @GetMapping("/list")
    public TableDataInfo list(Performance performance)
    {
        startPage();
        List<Performance> list = performanceService.selectPerformanceList(performance);
        return getDataTable(list);
    }

    @GetMapping(value = "/initLineAverageScore")
    public AjaxResult initLineAverageScore(Performance performance) {
        // 分数区间
        List<Performance> performances = performanceService.selectPerformanceList(new Performance());
        // 返回参数
        List<Performance> responseList = new ArrayList<>();
        int one = 0; // 0 - 60
        int two = 0; // 61 - 80
        int three = 0; // 81 - 100

        for (Performance item : performances) {
            if (item.getTotalPoints().compareTo(new BigDecimal(60)) < 0) {
                one++;
            } else if (item.getTotalPoints().compareTo(new BigDecimal(80)) < 0) {
                two++;
            } else {
                three++;
            }
        }

        Performance onePO = new Performance();
        onePO.setSection("0-60");
        onePO.setQty(one);
        responseList.add(onePO);

        Performance twoPO = new Performance();
        twoPO.setSection("61-80");
        twoPO.setQty(two);
        responseList.add(twoPO);

        Performance threePO = new Performance();
        threePO.setSection("81-100");
        threePO.setQty(three);
        responseList.add(threePO);

        return AjaxResult.success(responseList);
    }

    /**
     * 导出学生成绩列表
     */
    @PreAuthorize("@ss.hasPermi('system:performance:export')")
    @Log(title = "学生成绩", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Performance performance)
    {
        List<Performance> list = performanceService.selectPerformanceList(performance);
        ExcelUtil<Performance> util = new ExcelUtil<Performance>(Performance.class);
        util.exportExcel(response, list, "学生成绩数据");
    }

    /**
     * 获取学生成绩详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:performance:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(performanceService.selectPerformanceById(id));
    }

    /**
     * 新增学生成绩
     */
    @PreAuthorize("@ss.hasPermi('system:performance:add')")
    @Log(title = "学生成绩", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Performance performance)
    {
        return toAjax(performanceService.insertPerformance(performance));
    }

    /**
     * 修改学生成绩
     */
    @PreAuthorize("@ss.hasPermi('system:performance:edit')")
    @Log(title = "学生成绩", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Performance performance)
    {
        return toAjax(performanceService.updatePerformance(performance));
    }

    /**
     * 审核按钮
     * @param performance
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:performance:auth')")
    @Log(title = "学生成绩", businessType = BusinessType.UPDATE)
    @PutMapping(value = "auth")
    public AjaxResult auth(@RequestBody Performance performance)
    {
        return toAjax(performanceService.authPerformance(performance));
    }


    /**
     * 删除学生成绩
     */
    @PreAuthorize("@ss.hasPermi('system:performance:remove')")
    @Log(title = "学生成绩", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(performanceService.deletePerformanceByIds(ids));
    }

}
